package com.sskj.ffex.v.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.InputFilter;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidadvance.topsnackbar.TSnackbar;
import com.sskj.ffex.MyAppLication;
import com.sskj.ffex.R;
import com.sskj.ffex.common.App;
import com.sskj.ffex.common.MoneyValueFilter;
import com.sskj.ffex.common.MyTextWatcher;
import com.sskj.ffex.m.bean.UsefulCoin;
import com.sskj.ffex.m.bean.UserInfo;
import com.sskj.ffex.p.activity.TransferPresenter;
import com.sskj.ffex.utils.ClickUtil;
import com.sskj.ffex.utils.ToastUtil;
import com.sskj.ffex.v.base.BaseActivity;

import butterknife.BindView;


/**
 * 转账
 * create by Hey at 2018/8/7 9:54
 */
public class TransferActivity extends BaseActivity<TransferPresenter> {

    @BindView(R.id.transfer_toolbar)
    Toolbar toolBarLayout;
    @BindView(R.id.collapsing_toolbar)
    public CollapsingToolbarLayout mCollapsingToolbar;
    @BindView(R.id.transfer_id)
    EditText transferId;
    @BindView(R.id.edit_num)
    EditText editNum;
    @BindView(R.id.transfer_num)
    LinearLayout transferNum;
    @BindView(R.id.tranfer_useful)
    TextView transferUseful;
    @BindView(R.id.transfer_ps)
    EditText transferPS;
    @BindView(R.id.transfer_submit)
    Button transferSubmit;

    private float usefulNum;
    private UserInfo mUserInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_transfer;
    }

    @Override
    public TransferPresenter getPresenter() {
        return new TransferPresenter();
    }

    @Override
    protected void initView() {

        mUserInfo = (UserInfo) getIntent().getSerializableExtra("user");

        toolBarLayout.setNavigationOnClickListener(v -> finish());
        toolBarLayout.inflateMenu(R.menu.toolbar_menu);
//        Typeface typeface = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
//        mCollapsingToolbar.setExpandedTitleTypeface(typeface);
        toolBarLayout.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.toolbar_bill:
                        TransferHistoryActivity.start(mActivity);
                        break;
                }
                return true;
            }
        });
//        toolBarLayout.setLeftButtonOnClickLinster(view -> {
//            finish();
//        });
//        toolBarLayout.setRightButtonOnClickLinster(view -> {
//            TransferHistoryActivity.start(this);
//        });
        editNum.setOnFocusChangeListener((v, hasFocus) -> {
            transferNum.setActivated(hasFocus);
        });
        addWatcher(transferId);
        addWatcher(editNum);
        addWatcher(transferPS);
        editNum.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
        ClickUtil.click(transferSubmit, () -> {
            if (transferId.getText().toString().trim().equals(MyAppLication.getAccount())){
                ToastUtil.getInstance(MyAppLication.getApplication()).showMessage(App.INSTANCE.getString(R.string.strTransferActivity201));
                return;
            }

            if (!checkPwdStatus()) {
                return;
            }
            if (!checkIdent()) {
                return;
            }

            float num = Float.parseFloat(editNum.getText().toString());
            if (num > usefulNum) {
                showSnack(App.INSTANCE.getString(R.string.strTransferActivity202));
                return;
            }
            mPresenter.submitTransfer(transferId.getText().toString(), editNum.getText().toString(), transferPS.getText().toString());
        });
    }


    @Override
    protected void initData() {
        mPresenter.upDateUserInfo(MyAppLication.getMobile());
        mPresenter.getUsefulCoin();
    }

    public boolean checkSubmit() {
        if (checkEmpty(transferId) || checkEmpty(editNum) || checkEmpty(transferPS)) {
            return false;
        }
        return true;
    }

    public void addWatcher(EditText editText) {
        editText.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                transferSubmit.setEnabled(checkSubmit());
            }
        });
    }

    /**
     * 转账成功
     */
    public void submitSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.strTransferActivity203));
        TransferHistoryActivity.start(this);
    }

    public void submitFailure(String msg) {
        TSnackbar snackbar = TSnackbar.make(transferSubmit, msg, TSnackbar.LENGTH_LONG);
        snackbar.getView().setBackgroundColor(ContextCompat.getColor(this, R.color.red));
        TextView textView = snackbar.getView().findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text);
        textView.setTextColor(ContextCompat.getColor(this, R.color.white));
        snackbar.show();
    }

    public void setUsefulCoin(UsefulCoin data) {
        setText(transferUseful, App.INSTANCE.getString(R.string.strTransferActivity204) + data.getBalance());
        usefulNum = Float.parseFloat(data.getBalance());
    }


    public void showSnack(String msg) {
        TSnackbar snackbar = TSnackbar.make(transferSubmit, msg, TSnackbar.LENGTH_LONG);
        snackbar.getView().setBackgroundColor(ContextCompat.getColor(this, R.color.red));
        TextView textView = snackbar.getView().findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text);
        textView.setTextColor(ContextCompat.getColor(this, R.color.white));
        snackbar.show();
    }

    public boolean checkPwdStatus() {
        if (mUserInfo != null) {
            if (mUserInfo.tpwd != null && mUserInfo.tpwd.equals("1")) {
                return true;
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strTransferActivity206));
                startActivity(new Intent(mActivity, SecurityCenterActivity.class));
                return false;
            }
        }
        return false;
    }


    public void setUserInfo(UserInfo userinfo) {
        mUserInfo = userinfo;
    }

    public static void start(Context context, UserInfo userInfo) {
        Intent intent = new Intent(context, TransferActivity.class);
        intent.putExtra("user", userInfo);
        context.startActivity(intent);
    }
}
